package com.maze.game.model.base
{
	import flash.events.MouseEvent;

	public class NormalBase extends Base
	{
		public function NormalBase(baseWidth:uint, baseHeight:uint, type:uint = BaseType.EMPTY)
		{
			super(baseWidth, baseHeight, type);
			init();
		}
		
		private function init():void
		{
			drawBackground();
			switch(type)
			{
				case BaseType.WALL:
				{
					graphics.beginFill(0x000000);
					graphics.drawRect(0,0,baseWidth,baseHeight);
					graphics.endFill();
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
		
		private function addListeners():void
		{
			addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
		}
		
		private function removeListeners():void
		{
			removeEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		private function clickHandler(evt:MouseEvent):void
		{
			var dataChanged:Boolean = false;
			switch(type)
			{
				case BaseType.EMPTY:
				{
					type = BaseType.WALL;
					dataChanged = true;
					break;
				}
				case BaseType.WALL:
				{
					type = BaseType.EMPTY;
					dataChanged = true;
					break;
				}
			}
			init();
			if(dataChanged)
			{
				dispatchEvent(new BaseEvent(BaseEvent.TYPE_CHANGED));
			}
		}

		override public function set editable(value:Boolean):void
		{
			super.editable = value;
			buttonMode = editable;
			mouseChildren = editable;
			if(editable)
				addListeners();
			else
				removeListeners();
		}
	}
}